package cn.edu.sdjzu.cs.dingcan.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.edu.sdjzu.cs.dingcan.entity.Food;
import cn.edu.sdjzu.cs.dingcan.entity.ShoppingCart;
import cn.edu.sdjzu.cs.dingcan.service.FoodService;
import org.apache.commons.lang3.StringUtils;

@WebServlet("/shopping-cart")
public class ShoppingCartServlet extends HttpServlet {
    /**
     *
     */
    private static final long serialVersionUID = -1830545823467620287L;
    private final FoodService foodService = new FoodService();
    public static final String CART_SESSION_KEY = "shoppingCart";

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        if(null == request.getSession().getAttribute("customer")) { // 未登录
            response.sendRedirect(request.getContextPath() + "/customer-login?referer=" + request.getRequestURI());
            return;
        }
        String method = StringUtils.defaultIfBlank(request.getParameter("method"), "show-cart");
        try {
            switch(method) {
                case "add-item":
                    this.addItem(request, response);
                    break;
                case "remove-item":
                    this.removeItem(request, response);
                    break;
                case "alter-quantity":
                    this.changeItemQuantity(request, response);
                    break;
                case "show-cart":
                default:
                    this.showCart(request, response);
            }
        } catch (SQLException e) {
            throw new RuntimeException("执行数据库操作时遇到异常", e);
        }
        request.getRequestDispatcher("/app/detail/clientCart.jsp").forward(request, response);
    }

    public void showCart(HttpServletRequest request, HttpServletResponse response) {

    }

    public void addItem(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        Long foodId = Long.parseLong(request.getParameter("food-id"));
        Integer count = Integer.parseInt(request.getParameter("count"));
        HttpSession session = request.getSession();
        ShoppingCart cart = (ShoppingCart) session.getAttribute(CART_SESSION_KEY);
        if(cart == null) {
            cart = new ShoppingCart();
        }

        Food food = foodService.getById(foodId);
        cart.addItem(food, count);
        session.setAttribute(CART_SESSION_KEY, cart);
    }

    public void removeItem(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        Long foodId = Long.parseLong(request.getParameter("food-id"));
        HttpSession session = request.getSession();
        ShoppingCart cart = (ShoppingCart) session.getAttribute(CART_SESSION_KEY);
        Food food = foodService.getById(foodId);
        cart.removeItem(food);
        session.setAttribute(CART_SESSION_KEY, cart);
    }

    public void changeItemQuantity(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        Long foodId = Long.parseLong(request.getParameter("food-id"));
        Integer count = Integer.parseInt(request.getParameter("quantity"));
        HttpSession session = request.getSession();
        ShoppingCart cart = (ShoppingCart) session.getAttribute(CART_SESSION_KEY);
        Food food = foodService.getById(foodId);
        cart.addItem(food, count);
        session.setAttribute(CART_SESSION_KEY, cart);
    }
}
